﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using YouTube.Model;

namespace YouTube.Views
{
    public partial class VideoPlay : PhoneApplicationPage
    {
        public VideoPlay()
        {
            InitializeComponent();
        }
        private VideoViewModel viewmodel=new VideoViewModel();
        bool videoLoaded = false;
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            string Videoid = string.Empty;

            if (NavigationContext.QueryString.ContainsKey("VideoID"))
            {
                Videoid = NavigationContext.QueryString["VideoID"];
            }
            if (!videoLoaded)
            {
                LoadVideo(Videoid);
            }
            else
            {
                if (base.NavigationService.CanGoBack)
                {
                    base.NavigationService.GoBack();
                }
            }
            
        }
        public void LoadVideo(string id)
        {
            //YouTubeUrl=    new Uri("http://www.youtube.com/watch?v=7Q7vLqemc0w&feature=feedrec_grec_index");
            //YouTubeUrl.OriginalString
            //        .Remove("&feature=youtube_gdata")
            //        .Remove("http://www.youtube.com/watch?v=");
            VideoModel model = null;
            try
            {
                //YouTubeClient.Instance.getViDeos("ShreyaGoshal");
                bool flag;
                model = YouTubeClient.Instance.GetVideoFromId(new VideoId(id), this.PlayVideo, out flag);
                //this.viewmodel.Model = model;
                if (flag)
                {
                    this.PlayVideo(model);
                }
            }
            catch (Exception e)
            {
                if (base.NavigationService.CanGoBack)
                {
                    base.NavigationService.GoBack();
                }
            }

        }
        private void PlayVideo(VideoModel result)
        {
            Dispatcher.BeginInvoke(() =>
            {
                videoLoaded = true;
                this.viewmodel.Model = result;
                if (result != null)
                {

                    Microsoft.Phone.Tasks.MediaPlayerLauncher pl = new Microsoft.Phone.Tasks.MediaPlayerLauncher();
                    //pl.Media = result.MediaContentUriFormat2;
                    //pl.Show();
                    if (result.MediaContentUriFormat2 != null)
                    {
                        pl.Media = result.MediaContentUriFormat2; pl.Show();
                        //MediaPlayer.Source = result.MediaContentUriFormat2;
                    }
                    else if (result.MediaContentUriFormat3 != null)
                    {
                        pl.Media = result.MediaContentUriFormat3;pl.Show();
                       // MediaPlayer.Source = result.MediaContentUriFormat3;
                    }
                    else if (result.MediaContentUriFormat9 != null)
                    {
                        pl.Media = result.MediaContentUriFormat9;pl.Show();
                        // MediaPlayer.Source = result.MediaContentUriFormat9;
                    }
                    else
                    {
                        MessageBox.Show("Cannot Play back the file");
                    }
                    
                   // MediaPlayer.Play();
                }
            });
            //if (playVideoError != null)
            //{
            //    PersistentPlaybackState.Instance.CurrentVideo.LastKnownPlaybackState = PlaybackState.VideoFailedToPlay;
            //    this.ShowErrorMessage(ErrorString.ExceptionToYouTubeError(playVideoError));
            //}
            //else
            //{
            //    this.ProgressBar.Visibility = Visibility.Collapsed;
            //    this.ProgressBar.IsIndeterminate = false;
            //    VideoModel model = this.viewmodel.Model;
            //    if (((model != null) && (model.MediaContentUriFormat2 == null)) && ((model.MediaContentUriFormat3 == null) && (model.MediaContentUriFormat9 == null)))
            //    {
            //        PersistentPlaybackState.Instance.CurrentVideo.LastKnownPlaybackState = PlaybackState.VideoFailedToPlay;
            //        this.ShowErrorMessage(Strings.ErrorDialog_NoPvtAPIAvailable);
            //    }
            //    else
            //    {
            //        Uri uri = null;
            //        if ((NetworkInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) || (NetworkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet))
            //        {
            //            uri = model.MediaContentUriFormat3 ?? (model.MediaContentUriFormat9 ?? model.MediaContentUriFormat2);
            //        }
            //        else
            //        {
            //            uri = model.MediaContentUriFormat2 ?? (model.MediaContentUriFormat9 ?? model.MediaContentUriFormat3);
            //        }
            //        if (string.IsNullOrEmpty(this.vndQueryArgs))
            //        {
            //            this.MediaPlayer.Source = uri;
            //        }
            //        else if (uri.OriginalString.Contains("?"))
            //        {
            //            this.MediaPlayer.Source = new Uri(uri.OriginalString + "&" + this.vndQueryArgs);
            //        }
            //        else
            //        {
            //            this.MediaPlayer.Source = new Uri(uri.OriginalString + "?" + this.vndQueryArgs);
            //        }
            //        switch (PersistentPlaybackState.Instance.CurrentVideo.LastKnownPlaybackState)
            //        {
            //            case PlaybackState.NeverPlayed:
            //                if (PersistentPlaybackState.Instance.AppLaunchMode != LaunchMode.LaunchedNonHubWithVideo)
            //                {
            //                    this.StartInPausedState(0L);
            //                    break;
            //                }
            //                this.StartWithActivePlayback(0L);
            //                break;

            //            case PlaybackState.PausedByUser:
            //            case PlaybackState.TrickModeToEnd:
            //            case PlaybackState.RewoundToBeginning:
            //            case PlaybackState.VideoObscured:
            //            case PlaybackState.ScreenLocked:
            //            case PlaybackState.Tombstoned:
            //                this.StartInPausedState(PersistentPlaybackState.Instance.CurrentVideo.LastKnownPosition);
            //                break;

            //            case PlaybackState.PlayedToEnd:
            //                if (PersistentPlaybackState.Instance.AppLaunchMode != LaunchMode.LaunchedNonHubWithVideo)
            //                {
            //                    this.StartInPausedState(0L);
            //                    break;
            //                }
            //                this.StartWithActivePlayback(0L);
            //                break;

            //            case PlaybackState.VideoFailedToPlay:
            //                this.StartInPausedState(PersistentPlaybackState.Instance.CurrentVideo.LastKnownPosition);
            //                break;

            //            case PlaybackState.PlaybackActive:
            //                if (PersistentPlaybackState.Instance.AppLaunchMode != LaunchMode.LaunchedNonHubWithVideo)
            //                {
            //                    this.StartInPausedState(PersistentPlaybackState.Instance.CurrentVideo.LastKnownPosition);
            //                    break;
            //                }
            //                this.StartWithActivePlayback(0L);
            //                break;
            //        }
            //        this.SendToHub();
            //    }
            //}
        }
    }
}